package org.jeecg.modules.xxgx.fjys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.common.util.LoginUserUtil;
import org.jeecg.modules.innerApi.zhiYunSdApi.requestVo.SdFjxxBusinessParam;
import org.jeecg.modules.innerApi.zhiYunSdApi.responseVo.SdFjxxBusinessRtn;
import org.jeecg.modules.innerApi.zhiYunSdApi.responseVo.SdFjxxQueryBusinessMxRtn;
import org.jeecg.modules.innerApi.zhiYunSdApi.responseVo.SdFjxxQueryBusinessRtn;
import org.jeecg.modules.innerApi.zhiYunSdApi.service.IZhiyunSdfpService;
import org.jeecg.modules.xxgx.fjys.entity.CsXxCjmbFjys;
import org.jeecg.modules.xxgx.fjys.entity.CsXxFjys;
import org.jeecg.modules.xxgx.fjys.enums.FjysSjlxEnum;
import org.jeecg.modules.xxgx.fjys.mapper.CsXxFjysMapper;
import org.jeecg.modules.xxgx.fjys.service.ICsXxCjmbFjysService;
import org.jeecg.modules.xxgx.fjys.service.ICsXxFjysService;
import org.jeecg.modules.xxgx.kpzdgl.entity.CsXxKpzd;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 附加要素
 * @Author: jeecg-boot
 * @Date: 2023-10-09
 * @Version: V1.0
 */
@Service
public class CsXxFjysServiceImpl extends ServiceImpl<CsXxFjysMapper, CsXxFjys> implements ICsXxFjysService {


    @Autowired
    private ICsXxCjmbFjysService iCsXxCjmbFjysService;

    @Autowired
    private IZhiyunSdfpService iZhiyunSdfpService;


    @Override
    public void handleYyzt(List<CsXxFjys> fjysList) {
        for (int i = 0; i < fjysList.size(); i++) {
            CsXxFjys csXxFjys = fjysList.get(i);
            QueryWrapper<CsXxCjmbFjys> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("fjysid", csXxFjys.getId());
            int count = (int) iCsXxCjmbFjysService.count(queryWrapper);
            if (count > 0) {
                //有被引用
                csXxFjys.setYyzt("1");
                this.updateById(csXxFjys);
            } else {
                //没有被引用
                csXxFjys.setYyzt("0");
                this.updateById(csXxFjys);
            }
        }
    }

    @Override
    public void FjysSave(CsXxFjys csXxFjys) {
        List<CsXxKpzd> sd = LoginUserUtil.getKpzdsByUserKpzdsq("sd");
        LoginUser loginUser = LoginUserUtil.getLoginUser();
        if (sd.isEmpty()) {
            throw new JeecgBootException("当前操作员未分配数电终端！");
        }
        List<CsXxKpzd> collect = sd.stream().filter(i -> "在线".equals(i.getZxzt())).collect(Collectors.toList());
        if(collect.isEmpty()){
            throw new JeecgBootException("当前操作员终端都不在线！");
        }
        CsXxKpzd csXxKpzd = collect.get(0);
        SdFjxxBusinessParam sdFjxxBusinessParam = new SdFjxxBusinessParam();
        sdFjxxBusinessParam.setNsrsbh(csXxKpzd.getNsrsbh());
        sdFjxxBusinessParam.setBehavior("3");
        sdFjxxBusinessParam.setXxmc(csXxFjys.getXmmc());
        sdFjxxBusinessParam.setSjlx(FjysSjlxEnum.valueOf(csXxFjys.getSjlx()).getValue());
        sdFjxxBusinessParam.setCjr(loginUser.getRealname());
        sdFjxxBusinessParam.setKpzd(csXxKpzd);

        SdFjxxBusinessRtn rtn = iZhiyunSdfpService.fjxxBusiness(sdFjxxBusinessParam);

        if (rtn.getSuccess()) {
            csXxFjys.setFjysuuid(rtn.getData());
            this.save(csXxFjys);
        } else {
            throw new JeecgBootException(rtn.getMsg());
        }
    }

    @Override
    public void FjysEdit(CsXxFjys csXxFjys) {
        CsXxFjys fjys = this.getById(csXxFjys);
        if (StringUtils.isBlank(fjys.getFjysuuid())) {
            throw new JeecgBootException("Fjysuuid为空！");
        }
        List<CsXxKpzd> sd = LoginUserUtil.getKpzdsByUserKpzdsq("sd");
        LoginUser loginUser = LoginUserUtil.getLoginUser();
        if (sd.isEmpty()) {
            throw new JeecgBootException("当前操作员未分配数电终端！");
        }
        List<CsXxKpzd> collect = sd.stream().filter(i -> "在线".equals(i.getZxzt())).collect(Collectors.toList());
        CsXxKpzd csXxKpzd = collect.get(0);
        SdFjxxBusinessParam sdFjxxBusinessParam = new SdFjxxBusinessParam();
        sdFjxxBusinessParam.setNsrsbh(csXxKpzd.getNsrsbh());
        sdFjxxBusinessParam.setBehavior("4");
        sdFjxxBusinessParam.setXxmc(csXxFjys.getXmmc());
        sdFjxxBusinessParam.setSjlx(FjysSjlxEnum.valueOf(csXxFjys.getSjlx()).getValue());
        sdFjxxBusinessParam.setXgr(loginUser.getRealname());
        sdFjxxBusinessParam.setId(fjys.getFjysuuid());
        sdFjxxBusinessParam.setKpzd(csXxKpzd);
        SdFjxxBusinessRtn rtn = iZhiyunSdfpService.fjxxBusiness(sdFjxxBusinessParam);
        if (rtn.getSuccess()) {
            this.updateById(csXxFjys);
        } else {
            throw new JeecgBootException(rtn.getMsg());
        }

    }

    @Override
    public void FjysDelete(String id) {
        CsXxFjys fjys = this.getById(id);
        if (StringUtils.isBlank(fjys.getFjysuuid())) {
            throw new JeecgBootException("Fjysuuid为空！");
        }
        List<CsXxKpzd> sd = LoginUserUtil.getKpzdsByUserKpzdsq("sd");
        if (sd.isEmpty()) {
            throw new JeecgBootException("当前操作员未分配数电终端！");
        }
        List<CsXxKpzd> collect = sd.stream().filter(i -> "在线".equals(i.getZxzt())).collect(Collectors.toList());
        CsXxKpzd csXxKpzd = collect.get(0);
        SdFjxxBusinessParam sdFjxxBusinessParam = new SdFjxxBusinessParam();
        sdFjxxBusinessParam.setNsrsbh(csXxKpzd.getNsrsbh());
        sdFjxxBusinessParam.setBehavior("5");
        sdFjxxBusinessParam.setId(fjys.getFjysuuid());
        sdFjxxBusinessParam.setKpzd(csXxKpzd);
        SdFjxxBusinessRtn rtn = iZhiyunSdfpService.fjxxBusiness(sdFjxxBusinessParam);
        if (rtn.getSuccess()) {
            this.removeById(id);
        } else {
            throw new JeecgBootException(rtn.getMsg());
        }
    }

    @Override
    public void FjysBathDelete(String ids) {
        List<String> idlist = Arrays.asList(ids.split(","));
        List<CsXxKpzd> sd = LoginUserUtil.getKpzdsByUserKpzdsq("sd");
        if (sd.isEmpty()) {
            throw new JeecgBootException("当前操作员未分配数电终端！");
        }
        List<CsXxKpzd> collect = sd.stream().filter(i -> "在线".equals(i.getZxzt())).collect(Collectors.toList());
        CsXxKpzd csXxKpzd = collect.get(0);
        QueryWrapper<CsXxFjys> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id", idlist);
        List<CsXxFjys> fjysList = this.list(queryWrapper);
        SdFjxxBusinessParam sdFjxxBusinessParam = new SdFjxxBusinessParam();
        sdFjxxBusinessParam.setNsrsbh(csXxKpzd.getNsrsbh());
        sdFjxxBusinessParam.setBehavior("5");
        sdFjxxBusinessParam.setId(fjysList.stream().map(CsXxFjys::getFjysuuid).collect(Collectors.joining(",")));
        sdFjxxBusinessParam.setKpzd(csXxKpzd);
        SdFjxxBusinessRtn rtn = iZhiyunSdfpService.fjxxBusiness(sdFjxxBusinessParam);
        if (rtn.getSuccess()) {
            this.removeByIds(Arrays.asList(ids.split(",")));
        } else {
            throw new JeecgBootException(rtn.getMsg());
        }
    }

    @Override
    public void syncElecTax() {
        List<CsXxKpzd> sd = LoginUserUtil.getKpzdsByUserKpzdsq("sd");
        if (sd.isEmpty()) {
            throw new JeecgBootException("当前操作员未分配数电终端！");
        }
        List<CsXxKpzd> collect = sd.stream().filter(i -> "在线".equals(i.getZxzt())).collect(Collectors.toList());
        CsXxKpzd csXxKpzd = collect.get(0);
        SdFjxxBusinessParam sdFjxxBusinessParam = new SdFjxxBusinessParam();
        sdFjxxBusinessParam.setNsrsbh(csXxKpzd.getNsrsbh());
        sdFjxxBusinessParam.setBehavior("1");
        sdFjxxBusinessParam.setPage("1");
        sdFjxxBusinessParam.setSize("10000");
        sdFjxxBusinessParam.setKpzd(csXxKpzd);
        SdFjxxBusinessRtn rtn = iZhiyunSdfpService.fjxxBusiness(sdFjxxBusinessParam);
        if (rtn.getSuccess()) {
            //同步局端数据成功  开始查询
            SdFjxxBusinessParam sdFjxxBusinessParam1 = new SdFjxxBusinessParam();
            sdFjxxBusinessParam1.setNsrsbh(csXxKpzd.getNsrsbh());
            sdFjxxBusinessParam1.setBehavior("2");
            sdFjxxBusinessParam1.setPage("1");
            sdFjxxBusinessParam1.setSize("10000");
            sdFjxxBusinessParam1.setKpzd(csXxKpzd);
            SdFjxxQueryBusinessRtn rtn1 = iZhiyunSdfpService.fjxxQueryBusiness(sdFjxxBusinessParam1);
            if(rtn1.getSuccess()){
                List<SdFjxxQueryBusinessMxRtn> data = rtn1.getData();
                for (int i = 0; i < data.size(); i++) {
                    SdFjxxQueryBusinessMxRtn sdFjxxQueryBusinessMxRtn = data.get(i);
                    String xmmc = sdFjxxQueryBusinessMxRtn.getFjysxmmc();
                    //首先判断本地是否已经有了
                    QueryWrapper<CsXxFjys> queryWrapper = new QueryWrapper<>();
                    queryWrapper.eq("xmmc",xmmc);
                    queryWrapper.eq("company_id",csXxKpzd.getCompanyId());
                    int count = (int) this.count(queryWrapper);
                    if(count == 0){
                        CsXxFjys csXxFjys = new CsXxFjys();
                        csXxFjys.setFjysuuid(sdFjxxQueryBusinessMxRtn.getId());
                        csXxFjys.setXmmc(sdFjxxQueryBusinessMxRtn.getFjysxmmc());
                        csXxFjys.setSjlx(sdFjxxQueryBusinessMxRtn.getSjlx());
                        csXxFjys.setCompanyId(csXxKpzd.getCompanyId());
                        csXxFjys.setYyzt("0");
                        this.save(csXxFjys);
                    }
                }
            }else{
                throw new JeecgBootException(rtn1.getMsg());
            }
        } else {
            throw new JeecgBootException(rtn.getMsg());
        }

    }

    public static void main(String[] args) {
        System.out.println(FjysSjlxEnum.valueOf("string").getValue());
    }
}
